/**
 *  @random-map.js
 *  @version: 1.00
 *  @author: Jesse Freeman
 *  @date: May 2012
 *  @copyright based on iPhone RPG, (c) 2007 Chris Knight, Creative Commons license
 *
 *  This class creates a "Random Map" button in the weltmeister editor.
 *  It requires two layers, a collision layer named "collation" and your
 *  map layer named "main". Next to the button is a size value for the
 *  map. You main layer should have a tile map which will be used by the
 *  random map generator. The first tile is the background and the second
 *  tile is the ground. The generator will automatically create the collision
 *  tiles.
 *
 *  You can manually load this plugin by including it into the weltmeister.js
 *  requires block.
 *
 */
ig.module(
    'bootstrap.plugins.weltmeister.random-map-button'
)
    .requires(
        'bootstrap.plugins.weltmeister.random-map',
        'weltmeister.modal-dialogs'
)
    .defines(function ()
    {
        ModalDialogTest = wm.ModalDialog.extend({
            pathDropdown: null,
            pathInput: null,
            fileType: '',
            init: function (text, okText, type) {
                this.fileType = type || '';
                this.parent(text, (okText || 'Select'));
            },
            initDialog: function () {
                this.parent();

                this.buttonDiv.before("<dl>");

                // 
                this.buttonDiv.before('<dt>Map Size:</dt><dd><input type="text" class="number" id="mapSize" value="10"></dd>');

                // 
                this.buttonDiv.before('<dt>Room Size:</dt><dd><input type="text" class="number" id="roomSizeMin" value="1"> x <input type="text" class="number" id="roomSizeMax" value="3"></dd>');
                
                //
                this.buttonDiv.before('<dt>Wall Tiles:</dt><dd><input type="text" class="text" id="wallTiles" value="0"></dd>');
                this.buttonDiv.before('<dt>Floor Tiles:</dt><dd><input type="text" class="text" id="floorTiles" value="1"></dd>');

                //
                this.buttonDiv.before('<dt>Target Layer:</dt><dd><input type="text" class="text" id="mapLayerID" value="main"></dd>');
                //this.buttonDiv.before('<dt>Collision Layer ID:</dt><dd><input type="text" class="text" id="collisionLayerID" value="collision"></dd>');

            },
            clickOk: function () {
                console.log("Generate level");
                var mainLayerName = $("#mapLayerID").val();
                var collisionLayerName = "collision";//$("#collisionLayerID").val();
                var mainLayer = ig.editor.getLayerWithName(mainLayerName);
                var collisionLayer = ig.editor.getLayerWithName(collisionLayerName);


                // Create layers
                if (!mainLayer)
                    mainLayer = this.createLayer(mainLayerName);

                if (!collisionLayer)
                    collisionLayer = this.createLayer(collisionLayerName);

                var randomizer = new RandomMap($('#mapSize').val(), $('#roomSizeMax').val(), mainLayer, collisionLayer);

                ig.editor.setActiveLayer("entities");
                mainLayer.resetDiv();
                collisionLayer.resetDiv();

                this.close();
            },
            createLayer: function (name) {
                var tmpName = 'new_layer_' + ig.editor.layers.length;
                ig.editor.addLayer();
                var newLayer = ig.editor.getLayerWithName(tmpName);
                newLayer.setName(name);
                newLayer.resetDiv();
                return newLayer;
            }
        });
    });

//Add buttons to map editor
$('.headerFloat').prepend('<input type="button" id="randomize" value="Gernate Random Map" class="button"/>');
$('#randomize').bind('click', function ()
{
    if (!ig.editor.testDialog) {
        ig.editor.testDialog = new ModalDialogTest('Random Map Generator', 'Generate', 'scripts');
    }
    ig.editor.testDialog.open();
});


